<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<%@ include file="/common/common.jsp" %>
</head>
<body>
	<div class="content_wrap">
	<div class="zTreeDemoBackground left">
		<ul id="treeDemo" class="ztree"></ul>
	</div>
	</div>
	<script type="text/javascript">
		//创建配置
		var setting = {
			async: {
				enable: true,
				url: "<%=path%>/permissionsController/getzTree",
				autoParam: ["id", "name"]
			},
			data: {
				simpleData: {
					enable: true
				}
			},check: {
				enable: true,
				chkStyle: "checkbox",
				chkboxType: { "Y": "p", "N": "s" }
			},view : {
				addHoverDom: function(treeId, treeNode){
					var aObj = $("#" + treeNode.tId + "_a"); // tId = permissionTree_1, ==> $("#permissionTree_1_a")
					// aObj.attr("href", "javascript:;");
					if (treeNode.editNameFlag || $("#btnGroup"+treeNode.tId).length>0) return;
					var s = '<span id="btnGroup'+treeNode.tId+'">';
					if ( treeNode.level == 0 ) {
						s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode('+treeNode.id+')" href="#" >添加</a>';
					} else if ( treeNode.level == 1 ) {
						s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode('+treeNode.id+')" href="#" title="修改权限信息">修改</a>';
						if (treeNode.children == undefined) {
							s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode('+treeNode.id+')" href="#" >删除</a>';
						}
						s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode('+treeNode.id+')" href="#" >添加</a>';
					} else if ( treeNode.level == 2 ) {
						s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode('+treeNode.id+')" href="#" title="修改权限信息">修改</a>';
						s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode('+treeNode.id+')" href="#">删除</a>';
					}
					s += '</span>';
					aObj.after(s);
				},
				removeHoverDom: function(treeId, treeNode){
					$("#btnGroup"+treeNode.tId).remove();
				}
			}
		};



		//初始化树
		$(document).ready(function(){
			$.fn.zTree.init($("#treeDemo"), setting);
		});

		function addNode(fePid){
			BootstrapDialog.show({
				title: '增加',
				message: $('<div></div>').load('<%=path%>/permissionsController/addInfo'),
				/* type: BootstrapDialog.TYPE_SUCCESS, */
				size: BootstrapDialog.SIZE_SMALL,
				buttons: [{
					label: '取消',
					cssClass : "btn btn-info",
					action: function (dialog) {
						dialog.close();
					}
				}, {
					label: '确定',
					cssClass : "btn btn-info",
					action: function (dialog) {


						$.ajax({
							url:"<%=path%>/permissionsController/addInfos",
							data:{feName:$("#text").val(),feUrl:$("#feUrl").val(),fePid:fePid},
							dataType:"json",
							type:"post",
							async:true,
							success:function(result){
								if(result){
									dialog.close();
									toastr.success("增加成功");
									$.fn.zTree.init($("#treeDemo"), setting);
								}else{
									toastr.error("增加失败")
								}

							},
							error:function(){
								toastr.error("系统异常,请联系管理员");
							}
						})
					}
				}]
			});
		}

		function editNode(feId){
			BootstrapDialog.show({
				title: '修改',
				message: $('<div></div>').load('<%=path%>/permissionsController/getUpdateInfo/'+feId),
				/* type: BootstrapDialog.TYPE_SUCCESS, */
				size: BootstrapDialog.SIZE_SMALL,
				buttons: [{
					label: '取消',
					cssClass : "btn btn-info",
					action: function (dialog) {
						dialog.close();
					}
				}, {
					label: '确定',
					cssClass : "btn btn-info",
					action: function (dialog) {


						$.ajax({
							url:"<%=path%>/permissionsController/updateInfo",
							data:{feName:$("#text").val(),feUrl:$("#feUrl").val(),feId:feId},
							dataType:"json",
							type:"post",
							async:true,
							success:function(result){
								if(result){
									dialog.close();
									toastr.success("修改成功");
									$.fn.zTree.init($("#treeDemo"), setting);
								}else{
									toastr.error("修改失败")
								}

							},
							error:function(){
								toastr.error("系统异常,请联系管理员");
							}
						})
					}
				}]
			});
		}

		function deleteNode(feId){
			$.ajax({
				url:"<%=path%>/permissionsController/deleteInfo/"+feId,
				data:{},
				type:"post",
				async:false,//同步
				dataType:"json",
				success:function(result){
					if(result){
						toastr.success("删除成功");
						$.fn.zTree.init($("#treeDemo"), setting);
					}else{
						toastr.error("删除失败")
					}
				},
				error:function(){
					msg("系统错误，请联系管理员",3000);
				}
			})
		}
	</script>
</body>
</html>